package jdbc.util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;

/**
 * sqlsession工具包
 *
 * @author YunKaiLuo
 * @date 2024/9/27
 */
public class SqlSessionUtil {
    private SqlSessionUtil() {

    }

    private static SqlSessionFactory sqlSessionFactory = null;

    static {
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        try {
            sqlSessionFactory = sqlSessionFactoryBuilder
                    .build(Resources.getResourceAsReader("mybatis.xml"));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 返回会话
     * @return
     */
    public static SqlSession openSession() {
        return sqlSessionFactory.openSession();
    }
}
